var mouseX, mouseY;
var objX, objY;
var isDowm = false; //是否按下鼠标  
function mouseDown(e) {
	var obj = document.getElementById("window");
	//obj.style.cursor = "move";
	objX = obj.style.left;
	objY = obj.style.top;
	mouseX = e.clientX;
	mouseY = e.clientY;
	isDowm = true;
}
function mouseMove(e) {
	var div = document.getElementById("window");
	var x = e.clientX;
	var y = e.clientY;
	if (isDowm) {
		div.style.left = parseInt(objX) + parseInt(x) - parseInt(mouseX) + "px";
		div.style.top = parseInt(objY) + parseInt(y) - parseInt(mouseY) + "px";
		//document.getElementById("span1").innerHTML = "x:" + div.style.top + "  " + "y:" + div.style.left;  
	}
}
function mouseUp(e) {
	if (isDowm) {
		var x = e.clientX;
		var y = e.clientY;
		var div = document.getElementById("window");
		div.style.left = (parseInt(x) - parseInt(mouseX) + parseInt(objX))
				+ "px";
		div.style.top = (parseInt(y) - parseInt(mouseY) + parseInt(objY))
				+ "px";
		//document.getElementById("span2").innerHTML = "x:" + div.style.top + "  " + "y:" + div.style.left;  
		mouseX = x;
		rewmouseY = y;
		div.style.cursor = "default";
		isDowm = false;
	}
}

/**
 * 打开窗口方法
 * @param url
 * @param title
 * @param width
 * @param heigh
 * @param isCover
 * @param isAlert
 * @returns
 */
function openWindow(url,title,width,height,isCover,isAlert,isConfirm,endMethod) {
	var color = "#1E90FF";
	if (!color || color == null || color == "") {
		color = "#FF8C00";
	}
	//alert(url + title + width + height + isCover + isAlert);
	var bodyheight = document.documentElement.clientHeight - 20;
	var bodywidth = document.documentElement.clientWidth - 20;
	if (width < 100) {
		width = 100;
	}
	if (height < 100) {
		height = 100;
	}
	if (width > bodywidth) {
		width = bodywidth;
	}
	if (height > bodyheight) {
		height = bodyheight;
	}
	var left = (bodywidth - width)/2 + 10;
	var top = (bodyheight - height)/2 + 10;
	//alert(bodywidth + "   " + bodyheight);
	//alert(left + "   " + top);
	if (isCover) { //判断是否模态
		if (document.getElementById("windowcover")) {
			document.getElementById("windowcover").style.display="inline";
		} else {
			//遮罩层
			var cover = document.createElement("div");
			//外层div设置属性
			cover.id = "windowcover";
			cover.style.position = "fixed";
			cover.style.background = "gray";
			cover.style.width = (bodywidth + 20) + "px";
			cover.style.height = (bodyheight + 20) + "px";
			cover.style.top = "0";
			cover.style.left = "0";
			cover.style.filter = "fixed";
			//cover.style.-moz-opacity = "0.6";
			cover.style.opacity = "0.6";
			/*
			cover.innerHTML = '<div style="'
				+ 'position:fixed;background:gray;'
				+ 'width:' + (bodywidth + 20) + 'px;'
				+ 'height:' + (bodyheight + 20) + 'px;'
				+ 'top:0;left:0;'
				+ 'filter:alpha(Opacity=60);-moz-opacity:0.6;opacity:0.6;'
				+ '"></div>';
			*/
			document.body.appendChild(cover);
		}
	}
	//窗口
	var window = document.createElement("div");
	//窗口html
	var windowHtml
		//----------窗口div----------
		= '<div id="window"'
		+ 'style="position:fixed;'
		//+ 'background-color: #FF8C00;'
		//+ 'padding:1px;'
		//+ 'border-radius:5px 5px 0 0;'
		+ 'z-index:9999;'
		+ 'border:1px solid #000;'
		+ 'width:' + width + 'px;'
		+ 'height:' + height + 'px;'
		+ 'left:' + left + 'px;'
		+ 'top:' + top + 'px;"'
		+ '>'
		//---------头部div----------
	    + '<div id="windowTop"'
	    + ' style="background-color:'+color+'; position:absolute;'
	    + ' height:30px;width:100%;"'
	    //+ ' border-radius:5px 5px 0 0;"'
	    + ' onmousedown="mouseDown(event)"'
	    + ' onmousemove="mouseMove(event)"'
	    + ' onmouseup="mouseUp(event)">'
	    + '<a style="width:'+(width-30)+'px;height:30px;padding-left:10px;font-size:16px;font-weight:bold;line-height:30px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">'
	    + title
	    + '</a>'
	    + '<div style="width:30px;float:right;cursor:pointer;" onclick="closeWindow();"><img width="100%" src="/trm/javascript/window/close.png"/></div>'
	    + '</div>'
	    //---------内容div-----------
	    + '<div style="background-color:white;width:100%;height:100%;box-sizing:border-box;padding-top:30px;">';
	if (isAlert) {
		windowHtml += '<div style="box-sizing:content-box;height:23px;padding-top:'+(height-100)/2+'px;font-size:18px;text-align:center;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">';
		windowHtml += url;
		windowHtml += '</div>';
		if (isConfirm) {
			windowHtml += '<div style="width:140px;margin:0 auto;padding-top:5px;">';
			windowHtml += '<div style="margin:10px;width:50px;height:30px;float:left;text-align:center;font-size:13px;line-height:30px;background-color:'+color+';border-radius:5px;font-weight:bold;cursor:pointer;"';
			windowHtml += ' onclick="ok(' + endMethod + ',' + isConfirm + ');">确定</div>';
			windowHtml += '<div style="margin:10px;width:50px;height:30px;float:left;text-align:center;font-size:13px;line-height:30px;background-color:'+color+';border-radius:5px;font-weight:bold;cursor:pointer;" onclick="closeWindow();">取消</div>';
			windowHtml += '</div>';
		} else {
			windowHtml += '<div style="width:50px;height:30px;position:absolute;right:15px;bottom:15px;text-align:center;font-size:13px;line-height:30px;background-color:'+color+';border-radius:5px;font-weight:bold;cursor:pointer;"';
			windowHtml += ' onclick="ok(' + endMethod + ');">确定</div>';
		}
	} else {
		windowHtml += '<iframe src="/web/' + url + '"'
	    	+ 'width="100%" height="100%" frameborder="0"></iframe>'
	}
	windowHtml +='</div></div>';
	window.innerHTML = windowHtml;
	document.body.appendChild(window);
	//cover.appendChild(window);
}
function closeWindow() {
	if (document.getElementById("windowcover")) {
		document.getElementById("windowcover").style.display="none";
	}
	//document.getElementById("window" + id).style.display="none";
	var window = document.getElementById("window");
	document.body.removeChild(window.parentNode);
}

/**
 * 
 * @returns
 */
function ok(endMethod,isConfirm) {
	if (isConfirm) {
		return true
	}
	if (endMethod) {
		endMethod();
	}
	closeWindow();
}
/**
 * 在窗口打开页面
 * @returns
 */
function mmy_openWin() {
	//通过参数个数和类型实现方法重载
	if (arguments.length > 0) {
		var url = arguments[0];
		var title = "";
		var width = 900;
		var height = 500;
		var isCover = true;
		if (arguments[1] && typeof(arguments[1]) == "string") {
			title = arguments[1];
		}
		if (arguments[2] && typeof(arguments[2]) == "number") {
			width = arguments[2];
		}
		if (arguments[3] && typeof(arguments[3]) == "number") {
			height = arguments[3];
		}
		if (arguments[4] === false) {
			isCover = false;
		}
		openWindow(url,title,width,height,isCover,false,false,null);
	} else {
		return false;
	}
}
/**
 * 自定义弹窗
 * @param message
 * @returns
 */
function mmy_alert() {
	//通过参数个数和类型实现方法重载
	if (arguments.length > 0) {
		var message = arguments[0];
		var title = "提示";
		var width = 350;
		var height = 150;
		var isCover = true;
		var endMethod;
		if (arguments[1] && typeof(arguments[1]) == "string") {
			title = arguments[1];
		}
		if (arguments[2] === false) {
			isCover = false;
		}
		if (arguments[3] && typeof(arguments[3]) == "number") {
			width = arguments[3];
		}
		if (arguments[4] && typeof(arguments[4]) == "number") {
			height = arguments[4];
		}
		if (arguments[5] && typeof(arguments[5]) == "function") {
			method = arguments[5];
			method();
		}
		if (arguments[6] && typeof(arguments[6]) == "function") {
			endMethod = arguments[6];
		}
		openWindow(message,title,width,height,isCover,true,false,endMethod);
	} else {
		return false;
	}
}
/**
 * 自定义确认框
 * @returns
 */
function mmy_confirm() {
	var message = "确认执行此操作吗？";
	var title = "提示";
	var width = 350;
	var height = 150;
	var isCover = true;
	//通过参数个数和类型实现方法重载
	if (arguments.length > 0) {
		if (arguments[0] && typeof(arguments[0]) == "string") {
			message = arguments[0];
		}
		openWindow(message,title,width,height,isCover,true,true,null);
	} else {
		openWindow(message,title,width,height,isCover,true,true,null);
	}
}
